import count from '@dfeidao/atom-nodejs/db/count';
import query from '@dfeidao/atom-nodejs/db/query';
import format from '@dfeidao/atom-nodejs/dt/format';
import log from '@dfeidao/atom-nodejs/logger/log';
import { IncomingHttpHeaders } from 'http';
import { productid, systemid } from '../../atom/config';

interface Message {
	hidden_source: string;
	hidden_source_feature_no: string;
	pn: number;
}

interface IWebResult {
	data: unknown;
	cookie?: {
		[name: string]: string;
	} | null;
	content_type?: string;
	headers?: {
		[key: string]: string;
	};
	attachment?: string;
	redirect?: string;
	status_code?: number;
}
/* change_field	更改字段	string	0	√	ⅹ
change_before	更改前	string	32	√	ⅹ
change_after	更改后	string	32	√	ⅹ
change_personnel	更改人	string	50	√	ⅹ
change_date	更改时间	timestamp	50	√	ⅹ
hidden_source_feature_no	隐患源特性编号	string	50	√	ⅹ
 */
export default async function atom(message: Message, action_id: string, session_id: string, headers: IncomingHttpHeaders): Promise<IWebResult> {
	log('Service begin path:f:\实习\kh\src\wyf-hManager\zj-000010\s001,action_id:' + action_id);

	const { prepare, exec } = query(action_id, session_id, systemid);

	prepare('manage_data', ['change_field', 'change_before', 'change_after', 'change_personnel', 'change_date'], { productid, hidden_source: message.hidden_source, hidden_source_feature_no: message.hidden_source_feature_no }, 2, message.pn, [], []);

	const [res] = await exec<{ change_date: number; }>();
	const data = res.map((value) => {
		const date = format(value.change_date, 'YYYY-MM-DD');
		return { ...value, change_date: date };
	});

	const { prepare: p, exec: e } = count(action_id, session_id, systemid);/*(给prepare起了别名p)*/
	p('manage_data', { productid, hidden_source: message.hidden_source, hidden_source_feature_no: message.hidden_source_feature_no });
	const [c] = await e();

	log('Service end path:f:\实习\kh\src\wyf-hManager\zj-000010\s001,action_id:' + action_id);
	return {
		data: { data, c }
	};
}
